package queuedemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2023-12-09
 * Time: 22:50
 */
public class MyCircularQueue {
    //设计循环队列
    public int[] elem;  //有int型的数组
    public int front;   //队头索引,默认是0
    public int rear;    //队尾

    public MyCircularQueue(int k) {
        elem = new int[k];  //new一个,给k大小

    }

    //入队操作
    public boolean enQueue(int value) {
        //如果满了,就不许再添加
        if (isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;  //rear 向后移动一位
        return true;
    }

    //删除对头操作
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;  //和队尾删除一样
        return true;
    }

    //得到对头元素,不删除
    public int Front() {
        if (isEmpty()) {  //如果是空,返回-1
            return -1;
        }
        return elem[front];
    }

    //得到队尾元素,
    //有特殊情况
    public int Rear() {
        if (isEmpty()) {  //如果是空,返回-1
            return -1;
        }
        int index = (rear == 0) ? elem.length - 1 : rear - 1;
        return elem[index];
    }

    //判断是否为空
    public boolean isEmpty() {
        if (front == rear) {  //如果队头和队尾相遇就判空
            return true;
        }
        return false;
    }

    //判断是否满了
    public boolean isFull() {
        return (rear + 1) % elem.length == front;  //队尾+1对总数取模等于队头就说明队列满了
    }
}
